<!--
 * @Description: x6 树节点
-->
<template>
  <div
    class="basics-card"
    :style="!props.state.isDetail ? basicsCardStyle2 : props.state.departmentName ? basicsCardStyle : basicsCardStyle1"
    @click="handleSelectNode"
  >
    <div
      class="trapezoid"
      style="
        position: absolute;
        left: 0px;
        top: 11px;
        width: 4px;
        height: 40px;
        background: #213c8d;
        transform: perspective(0.5em) rotateY(-3deg);
      "
      :style="{ top: props.state.departmentName ? '11px' : '32px' }"
    ></div>
    <div
      class="card-header"
      style="display: flex; justify-content: space-between; margin-top: 7px"
      v-if="props.state.departmentName"
    >
      <div
        class="header-left"
        style="color: rgba(29, 33, 41, 1); font-size: 22px; line-height: 150%; margin-left: 12px; font-weight: bold"
      >
        {{ props.state.departmentName }}
      </div>
      <div
        class="header-right"
        v-if="props.state.deptLevel"
        style="
          width: 78px;
          height: 25px;
          background-color: rgb(243, 248, 252);
          color: rgb(78, 89, 105);
          font-size: 16px;
          text-align: center;
          border-radius: 2px;
          margin-right: 12px;
        "
      >
        {{ props.state.deptLevel }}
      </div>
      <div class="w-[65px] h-[16px]" v-else style="width: 78px; height: 25px"></div>
    </div>
    <div class="card-main" style="display: flex; margin: 8px; margin-top: 19px">
      <div class="head-portrait" style="margin-right: 15px; width: 74px; height: 74px; border-radius: 50%; margin-left: 12px">
        <img
          v-if="props.state.avatar"
          style="width: 74px; height: 74px; border-radius: 50%"
          src="http://hcm.ruijie.com.cn/ruijie/attachment/Content/87b9b6d2-ce22-4301-985a-aecf00dc2afe"
          alt=""
        />
        <img v-else style="width: 74px; height: 74px; border-radius: 50%" src="@/assets/icons/touxiangdoudi.png" alt="" />
      </div>
      <div class="card-main-right" style="margin-right: 20px">
        <div
          class="name"
          style="height: 30px; color: rgba(78, 89, 105, 1); font-size: 18px; line-height: 150%; font-weight: bold"
        >
          {{ props.state.portionManageName || props.state.userName }}
        </div>
        <div
          class="title"
          style="
            margin-top: 7px;
            width: 90px;
            height: 18px;
            color: rgba(134, 144, 156, 1);
            font-size: 16px;
            line-height: 150%;
            text-align: left;
          "
        >
          {{ props.state.position }}
        </div>
      </div>
      <div v-if="!props.state.isDetail">
        <div
          style="
            height: 30px;
            color: rgba(78, 89, 105, 1);
            font-size: 18px;
            line-height: 150%;
            font-weight: bold;
            width: 100px;
            white-space: nowrap;
            overflow: hidden;
            text-overflow: ellipsis;
          "
        >
          {{ props.state.headcount || 0 }}/{{ props.state.establishmentCount || 0 }}
        </div>
        <div
          style="
            margin-top: 7px;
            width: 130px;
            height: 18px;
            color: rgba(134, 144, 156, 1);
            font-size: 16px;
            line-height: 150%;
            text-align: left;
          "
        >
          总人数/编制数
        </div>
      </div>
    </div>
    <div
      class="card-footer"
      @mousedown="handleMouseDown"
      @mouseup="handleMouseUp"
      style="margin-top: 8px; min-height: 100px; width: 100%"
    >
      <div class="footer-left" style="height: 100%; display: flex; flex-direction: column">
        <div v-if="props.state.isDetail" class="footer-d" style="display: flex; justify-content: end; height: 30px">
          <img class="w-[15px] h-[15px]" src="@/assets/icons/ddd.png" alt="" style="width: 30px; height: 30px" />
        </div>
        <div
          class="footer-mian"
          v-if="props.state.isDetail"
          style="
            flex: 1;
            display: flex;
            justify-content: flex-start;
            background-color: rgba(243, 248, 252, 1);
            padding: 8px;
            overflow: hidden;
            border: 1px solid #e5e6eb;
            box-sizing: border-box;
          "
        >
          <div class="footer-mian-smell" :style="footerMianSmell">
            <div
              class="number"
              style="
                font-size: 14px;
                font-weight: bold;
                color: #4e5969;
                width: 50px;
                white-space: nowrap;
                overflow: hidden;
                text-overflow: ellipsis;
              "
            >
              {{ props.state.headcount || 0 }}
            </div>
            <div class="footer-footer" style="display: flex; box-sizing: border-box; padding-right: 6px; margin-top: 5px">
              <div class="title" style="font-size: 14px; color: rgba(134, 144, 156, 1)">总人数</div>
            </div>
          </div>
          <div class="footer-mian-smell" :style="footerMianSmell">
            <div
              class="number"
              style="
                font-size: 14px;
                font-weight: bold;
                color: #4e5969;
                width: 50px;
                white-space: nowrap;
                overflow: hidden;
                text-overflow: ellipsis;
              "
            >
              {{ getMoneyDisplay(props.state, props.state.establishmentCount) }}
            </div>
            <div class="footer-footer" style="display: flex; box-sizing: border-box; padding-right: 6px; margin-top: 5px">
              <div class="title" style="font-size: 14px; color: rgba(134, 144, 156, 1)">编制数</div>
            </div>
          </div>
          <div class="footer-mian-smell" :style="footerMianSmell">
            <div
              class="number"
              style="
                font-size: 14px;
                font-weight: bold;
                color: #4e5969;
                width: 50px;
                white-space: nowrap;
                overflow: hidden;
                text-overflow: ellipsis;
              "
            >
              {{ getMoneyDisplay(props.state, props.state.waitIn) }}
            </div>
            <div class="footer-footer" style="display: flex; box-sizing: border-box; padding-right: 6px; margin-top: 5px">
              <div class="title" style="font-size: 14px; color: rgba(134, 144, 156, 1)">待入职</div>
            </div>
          </div>
          <div class="footer-mian-smell" :style="footerMianSmell">
            <div
              class="number"
              style="
                font-size: 14px;
                font-weight: bold;
                color: #4e5969;
                width: 50px;
                white-space: nowrap;
                overflow: hidden;
                text-overflow: ellipsis;
              "
            >
              {{ getMoneyDisplay(props.state, props.state.duringOut) }}
            </div>
            <div class="footer-footer" style="display: flex; box-sizing: border-box; padding-right: 6px; margin-top: 5px">
              <div class="title" style="font-size: 14px; color: rgba(134, 144, 156, 1)">离职中</div>
            </div>
          </div>
          <div
            style="
              flex: 0 1 auto;
              text-align: center;
              background-color: rgba(243, 248, 252, 1);
              white-space: nowrap;
              padding: 0 10px;
            "
          >
            <div
              class="number"
              style="
                font-size: 14px;
                font-weight: bold;
                color: #4e5969;
                width: 70px;
                white-space: nowrap;
                overflow: hidden;
                text-overflow: ellipsis;
              "
            >
              {{ getMoneyDisplay(props.state, props.state.money) }}
            </div>
            <div
              class="footer-footer none"
              style="display: flex; border-right: none; box-sizing: border-box; padding-right: 6px; margin-top: 5px"
            >
              <div class="title" style="font-size: 14px; color: rgba(134, 144, 156, 1)">成本/预算(w)</div>
            </div>
          </div>
        </div>
      </div>
    </div>
    <div
      v-if="props.state.overEstablishment"
      class="show-box"
      style="position: absolute; width: 0; height: 0; left: 4px; bottom: 4px; width: 59px; height: 59px"
    >
      <img style="width: 59px; height: 59px" src="@/assets/icons/chao.png" alt="" />
    </div>
  </div>
  <div v-if="props.state.isHaveChildren" :style="childCircleStyle" class="tree-collaps-ablenode" @click="handleClickAddChildren">
    <img src="@/assets/icons/open.png" alt="" />
    <div :style="collapsableNodeAfterStyle" class="tree-collaps-ablenode-after"></div>
  </div>
</template>

<script setup lang="ts">
import { ref } from "vue";
import { cloneDeep } from "lodash-es";

const props = defineProps({
  state: {
    type: Object,
    default: () => ({ id: "" as string })
  }
});

const emits = defineEmits(["handleClick", "handleDelete", "handleAddChildren", "handleSelectNode", "handleAddChildren"]);

const mouseDownTime = ref<number>(0);
const handleClickAddChildren = () => {
  console.log(props.state, "props.state");
  emits("handleAddChildren", props.state);
};
const basicsCardStyle = {
  width: "100%",
  height: "265px",
  minHeight: "151px",
  fontSize: "24px",
  position: "relative",
  display: "flex",
  flexDirection: "column",
  padding: "5px 6px",
  border: "1px solid #e5e6eb",
  background: "rgba(255, 255, 255, 1)",
  boxSizing: "border-box"
};
const basicsCardStyle1 = {
  width: "100%",
  height: "217px",
  minHeight: "151px",
  fontSize: "24px",
  position: "relative",
  display: "flex",
  flexDirection: "column",
  padding: "0px 6px 5px",
  border: "1px solid #e5e6eb",
  background: "rgba(255, 255, 255, 1)",
  boxSizing: "border-box"
};
const basicsCardStyle2 = {
  width: "100%",
  height: "214px",
  minHeight: "151px",
  fontSize: "24px",
  position: "relative",
  display: "flex",
  flexDirection: "column",
  padding: "5px 6px",
  border: "1px solid #e5e6eb",
  background: "rgba(255, 255, 255, 1)",
  boxSizing: "border-box"
};

const footerMianSmell = {
  padding: "0 11px",
  textAlign: "center"
};
const collapsableNodeAfterStyle = {
  content: "",
  position: "absolute",
  top: "-32px",
  left: "16px",
  width: "2px",
  height: "30px",
  backgroundColor: "#8f8f8f"
};
const childCircleStyle = {
  borderRadius: "50%",
  width: "40px",
  height: "40px",
  marginLeft: "194px",
  marginTop: "30px",
  position: "relative",

  color: "rgb(255, 255, 255)",
  backgroundColor: "rgb(33, 60, 141)",
  fontSize: "19px",
  display: "flex",
  justifyContent: "center",
  alignItems: "center",
  border: "rgb(255, 255, 255) solid 3px",
  fontWeight: "bold",
  zIndex: 10,
  cursor: "pointer"
};
const handleSelectNode = () => {
  emits("handleSelectNode", cloneDeep(props.state));
};

// 点击卡片操作
const handleDetail = () => {
  emits("handleClick", cloneDeep(props));
};

const handleMouseDown = e => {
  mouseDownTime.value = Date.now();
};
const handleMouseUp = e => {
  const duration = Date.now() - mouseDownTime.value;
  if (duration > 1000) {
    console.log("大于一秒默认拖拽");
  } else {
    handleDetail();
  }
};
const getMoneyDisplay = (state, item) => {
  // 检查 state 和 money 是否有值
  if (state && item !== undefined && item !== null) {
    return item;
  }
  // 如果没有值，返回默认的 '--'
  return "0";
};

// const rightBorder = {
//   content: '',
//   position: 'absolute',
//   top: '49%',
//   rigth: '0',
//   width: '2px',
//   height: '33%',
//   backgroundColor: '#rgba(134, 144, 156, 1)'
// }
</script>
<style lang="scss" scoped>
.footer-mian-smell {
  position: relative;
}
.footer-mian-smell::after {
  content: "";
  position: absolute;
  top: 49%;
  right: 0;
  width: 2px;
  height: 33%;
  background-color: rgba(134, 144, 156, 1);
}
</style>
